import java.awt.*;
import javax.swing.*;
import java.util.LinkedList;
/**
 * Class to handle all graphics
 * 
 * @author Jofry HS
 * @version 16-07-2010
 */
public class ImagePanel extends JPanel
{    
    // Width and Height of one tile
    private final static int CELL_WIDTH = 30;
    private final static int CELL_HEIGHT = 30;
    
    // All images are loaded from specific folder
    Image grassImg = Toolkit.getDefaultToolkit().getImage("img/grass.png");
    Image badbotImg = Toolkit.getDefaultToolkit().getImage("img/badbot.png");
    Image goodbotImg = Toolkit.getDefaultToolkit().getImage("img/goodbot.png");
    Image homeImg = Toolkit.getDefaultToolkit().getImage("img/home.png");
    Image obstacleImg = Toolkit.getDefaultToolkit().getImage("img/obstacle.png");
    
    // ImagePanel have access to the maze
    private Maze maze;
    
    /**
     * Constructor for objects of class ImagePanel
     */
    public ImagePanel(Maze m)
    {
        maze = m;       
        
        // Set the size to allow dynamic adjustment
        this.setPreferredSize(new Dimension(m.getWidth()*30,m.getHeight()*30));
    }
    
    // The reason why we have this class
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        
        // Get the information of the map
        Tile [][] map = maze.getMap();
        for (int row = 0; row < maze.getWidth(); row++)
        {
            for (int col = 0; col < maze.getHeight(); col++)
            {
                int cellRow = row * CELL_WIDTH;
                int cellCol = col * CELL_HEIGHT;
                
                // If it's passable then it's green grass, else we put rock on it
                if (map[row][col].isPassable())
                    g.drawImage(grassImg, cellRow, cellCol, this);       
                else
                    g.drawImage(obstacleImg, cellRow, cellCol, this);
            }
        }
        
        // Paint all the bad bots location
        LinkedList<Robot> badBots = maze.getBadBots();
        for (int i = 0; i < badBots.size(); i++)
        {
            Robot bad = badBots.get(i);
            int cellRow = bad.getX() * CELL_WIDTH;
            int cellCol = bad.getY() * CELL_HEIGHT;
            g.drawImage(badbotImg, cellRow, cellCol, this);
        }
        
        // Paint the goal location
        g.drawImage(homeImg, maze.getGoalX() * CELL_WIDTH, maze.getGoalY() * CELL_HEIGHT, this);
        
        // Paint our hero bot location
        int cellRow = maze.getGoodBot().getX() * CELL_WIDTH;
        int cellCol = maze.getGoodBot().getY() * CELL_HEIGHT;
        g.drawImage(goodbotImg, cellRow, cellCol, this);
               
        // According to the game state paint some string to let the user know
        if(maze.getCondition() == maze.CONDITION_WIN)
        {              
            g.setColor(Color.BLACK);
            Font font = new Font("Helvetica", Font.BOLD,  10 + maze.getWidth());
            g.setFont(font);
            g.drawString("You CLEARED this stage!", maze.getWidth()/3 * CELL_WIDTH, maze.getHeight()/2 * CELL_HEIGHT);
        }  
        else if(maze.getCondition() == maze.CONDITION_LOSE)
        {   
            g.setColor(Color.RED);
            Font font = new Font("Helvetica", Font.BOLD,  10 + maze.getWidth() * 2);
            g.setFont(font);
            g.drawString("Uh-oh! Got caught!", maze.getWidth()/3 * CELL_WIDTH, maze.getHeight()/2 * CELL_HEIGHT);
        }  
        
    }    
}
